<template>
  <div class="login-wrapper">
    <!-- 顶部tarBar -->
    <van-nav-bar
      title="登录"
      left-arrow
      fixed
      @click-left="$router.go(-1)"
      class="animate__fadeInDown animate__animated"
    />
    <img class="icon" src="../../../assets/image/icon.png" alt="">
    <!-- 顶部tarBar -->
    <div class="form-wrapper">
      <van-form >
        <van-field
          v-model="username"
          placeholder="请输入账号"
          label="用户名"
          :rules="[{ pattern:/^[a-zA-Z0-9]{4,10}$/, message: '请输入正确的用户名' }]"
        />
        <!-- 不能含有非法字符，长度在4-10之间 -->
        <van-field
          v-model="password"
          placeholder="请输入密码"
          label="密码"
          :rules="[{ pattern:/^[a-zA-Z0-9]{4,10}$/, message: '请输入正确的密码' }]"
        />
      </van-form>
      <div class="register-login">
        <van-button class="register-btn" type="default" @click="register">注册</van-button>
      <van-button class="login-btn" type="default" @click="login">登录</van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { register,login } from '../../../api/login'
import qs from 'qs'
export default {
  name: "Login",
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    // 注册
    async register() {
      this.$toast.loading("注册中")
      const res = await register(qs.stringify({username:this.username,password:this.password}))
      if (res.data.status === 1) {
        this.$toast(res.data.message)
      }
      if (res.data.status === 0) {
        this.$toast.success("注册成功，快去登录吧！")
      }
    },
    // 登录
    async login () {
      this.$toast.loading("登录中")
      const res = await login(qs.stringify({username:this.username,password:this.password}))
      if (res.data.status === 1) {
        this.$toast.fail(res.data.message)
      }
      if (res.data.status === 0) {
        this.$toast.success("登录成功")
        // 储存token
        this.$store.commit("changeToken",res.data.token)
        // 储存username
        this.$store.commit("setUsername",res.data.username)
        // 跳转页面
        this.$router.push(this.$store.state.prePage)
      }
    }
  },
};
</script>

<style lang="scss" scoped>
.login-wrapper {
  background-color: rgb(245, 245, 245);
  width: 100vw;
  height: 100vh;
  position: relative;
  .icon {
    position: absolute;
    width: 100px;
    height: 100px;
    top: 100px;
    left: 137.5px;
  }
  .form-wrapper {
    width: 355px;
    position: absolute;
    top: 250px;
    left: 10px;
    border-radius: 10px;
    overflow: hidden;
    .register-login {
      width: 100%;
      height: 80px;
      background-color: #fff;
      display: flex;
      justify-content: space-between;
      align-items: center;
      
      .register-btn {
        width: 100px;
        margin-left: 30px;
        background-color: #546de5;
        color: rgb(240, 240, 240);
        border-radius: 5px;
      }
      .login-btn {
        width: 100px;
        margin-right:30px ;
        background-color: #e66767;
        color: rgb(240, 240, 240);
        border-radius: 5px;
      }
    }
  }
}
</style>